package br.senai.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.senai.entity.Materia;
import br.senai.service.MateriaService;
import br.senai.service.ProfessorService;


@WebServlet("/materia")
public class MateriaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Long id = Long.valueOf(request.getParameter("codigoMateria"));	
		MateriaService ms = new MateriaService();
		Materia materia = ms.buscarMateria(id);
		
		if(!materia.equals(null)){
			if(id != 0){
			ms.editarMateria(materia);
			}
		} else {
				materia.setNomeMateria(request.getParameter("nome"));
				materia.setDescricao(request.getParameter("descricao"));
				materia.getProfessor().setId_professor(Long.valueOf(request.getParameter("professorMateria")));;
				ms.adicionarMateria(materia);
		}
		RequestDispatcher rd = request.getRequestDispatcher("ListaMateria");
		rd.forward(request, response);
	}

}
